<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Cache;
class ProductLists extends Common {
	
	public function index(){
		$id = (int) request()->param('cid');
		$cate = Db::name('category')->where(['id'=>$id])->find();
		if(!isset($cate['template_list'])){
			return $this->fetch(config('template.default_them').'/404');
		}
		// p($id);die;
		$d = $this->getCateData($id);
		$list = Db::name('product')->where('cid','in',$d)->paginate(10, false, ['query' => request()->param()]);
		if(!$list){
			return $this->fetch(config('template.default_them').'/404');
		}
		
		// p($list);
		$this->assign('list', $list);
		return $this->fetch('template/'.config('template.default_them').'/'.$cate['template_list']);
	}
	//处理所属分类
	public function getCateData($id)
	{
		$data = db('category')->select();
	    //1.先找出所有子集
	    $ids = $this->getSon($data, $id);
	    //2.把自己追加进去
	    $ids[] = $id;
	    //3.获取除了自己和自己子集的全部数据
	    // $map['id'] = array('not in', $ids);
	    // $data = db('category')->where($map)->select();
		// var_dump($ids);die;
	    return $ids;
	}
	
	//递归找子集
	public function getSon($data, $id)
	{
	    static $temp = [];
	    foreach ($data as $k => $v) {
	        if ($v['pid'] == $id) {//这个时候说明你是$cid的儿子
	            $temp[] = $v['id'];
	            $this->getSon($data, $v['id']);
	        }
	    }
	    return $temp;
	}
}